package com.system.manager.modules.projectLibrary.controller;

import com.system.manager.common.annotation.Anonymous;
import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.Result;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.projectLibrary.dto.UsernamePassword;
import com.system.manager.modules.projectLibrary.entity.ProjectLeader;
import com.system.manager.modules.projectLibrary.service.IProjectLeaderService;
import com.system.manager.modules.projectLibrary.vo.ProjectLeaderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.usertype.EnhancedUserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jisf
 * @since 2021-11-03
 */
@Api(tags = "项目负责人API")
@RestController
@RequestMapping("/projectLibrary/project-leader")
public class ProjectLeaderController {

    @Autowired
    private IProjectLeaderService iProjectLeaderService;

    @ApiOperation("项目负责人注册个人信息")
    @Anonymous
    @PostMapping("/projectLeader/register")
    public Result projectLeaderRegister(@RequestBody ProjectLeaderVo projectLeaderVo) {
        try {
            UsernamePassword usernamePassword = iProjectLeaderService.projectLeaderRegister(projectLeaderVo);
            return Result.ok(usernamePassword);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("修改用户信息")
    @PreAuthorize(SubjectType.PROJECT_LEADER)
    @PostMapping("/update/projectLeader")
    public Result updateProjectLeader(@RequestBody ProjectLeader projectLeader, @CurrSubject Subject subject) {
        try {
            iProjectLeaderService.updateProjectLeader(projectLeader, subject);
            return Result.ok();
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("激活项目负责人")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/activate/projectLeader")
    public Result activateProjectLeader(@RequestParam Integer id) {
        try {
            iProjectLeaderService.activateProjectLeader(id);
            return Result.ok("激活成功");
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("取消激活项目负责人")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/deactivate/projectLeader")
    public Result deactivateProjectLeader(@RequestParam Integer id) {
        try {
            iProjectLeaderService.deactivateProjectLeader(id);
            return Result.ok("取消成功");
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("项目负责人取消分配")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/assign/project")
    public Result assignProject(@RequestParam Integer id) {
        try {
            String s = iProjectLeaderService.assignProject(id);
            return Result.ok(s);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("查询项目负责人")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/select/projectLeader")
    public Result selectProjectLeader(@CurrSubject Subject subject,
                                      @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                      @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,
                                      @RequestParam(name = "name", required = false) String name,
                                      @RequestParam(name = "project", required = false) Integer project,
                                      @RequestParam(name = "activate", required = false) Integer activate,
                                      @RequestParam(name = "whetherFree", required = false) Integer whetherFree) {
        try {
            Result result = iProjectLeaderService.selectProjectLeader(subject, page, size, activate, name, project, whetherFree);
            return result;
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
}
